package com.llb666.shop.order.infrastructure.persistence;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llb666.common.core.enums.OrderStatusEnum;
import com.llb666.common.core.enums.YesOrNo;
import com.llb666.common.core.utils.page.PagedGridResult;
import com.llb666.shop.order.api.vo.center.MyOrdersVo;
import com.llb666.shop.order.domain.entity.OrderStatus;
import com.llb666.shop.order.domain.entity.Orders;
import com.llb666.shop.order.domain.repository.OrdersRepository;
import com.llb666.shop.order.infrastructure.conver.OrderCvt;
import com.llb666.shop.order.infrastructure.conver.OrderStatusCvt;
import com.llb666.shop.order.infrastructure.mapper.OrderStatusMapper;
import com.llb666.shop.order.infrastructure.mapper.OrdersMapper;
import com.llb666.shop.order.infrastructure.po.OrderStatusPo;
import com.llb666.shop.order.infrastructure.po.OrdersPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单仓储层实现类.
 *
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class OrdersRepositoryImpl implements OrdersRepository {

    private final OrdersMapper ordersMapper;
    private final OrderStatusMapper orderStatusMapper;

    /**
     * 更新订单状态为"已发货，待收货"".
     *
     * @param orderId 订单id
     */
    @Override
    public void updateDeliverOrderStatus(String orderId) {
        // 对象待修改的字段.
        OrderStatusPo orderStatusPo = new OrderStatusPo();
        orderStatusPo.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
        orderStatusPo.setDeliverTime(new Date());
        // 更新筛选条件.
        Example example = new Example(OrderStatusPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);

        orderStatusMapper.updateByExampleSelective(orderStatusPo, example);
    }

    /**
     * 更新订单状态为"交易成功".
     *
     * @param orderId 订单id
     * @return true/false
     */
    @Override
    public boolean updateReceiveOrderStatus(String orderId) {
        // 对象待修改的字段.
        OrderStatusPo updateOrderPo = new OrderStatusPo();
        updateOrderPo.setOrderStatus(OrderStatusEnum.SUCCESS.type);
        updateOrderPo.setSuccessTime(new Date());
        // 更新筛选条件.
        Example example = new Example(OrderStatusPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        criteria.andEqualTo("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);

        int result = orderStatusMapper.updateByExampleSelective(updateOrderPo, example);
        return result == 1;
    }

    /**
     * 删除订单
     *
     * @param userId  用户id
     * @param orderId 订单id
     * @return true/false
     */
    @Override
    public boolean deleteOrder(String userId, String orderId) {
        OrdersPo delOrder = new OrdersPo();
        delOrder.setIsDelete(YesOrNo.YES.type);
        delOrder.setUpdatedTime(new Date());

        Example example = new Example(Orders.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", orderId);
        criteria.andEqualTo("userId", userId);

        int result = ordersMapper.updateByExampleSelective(delOrder, example);
        return result == 1;
    }

    /**
     * 查询我的订单 -- 已评价.
     *
     * @param userId  用户id
     * @param orderId 订单id
     * @return 订单
     */
    @Override
    public Orders queryMyOrder(String userId, String orderId) {
        OrdersPo orders = new OrdersPo();
        orders.setUserId(userId);
        orders.setId(orderId);
        orders.setIsComment(YesOrNo.NO.type);
        return OrderCvt.INSTANCE.po2do(ordersMapper.selectOne(orders));
    }

    /**
     * 更新订单为"已评价".
     *
     * @param orderId 订单id
     */
    @Override
    public void updateCommentsStatus(String orderId) {
        OrdersPo order = new OrdersPo();
        order.setId(orderId);
        order.setIsComment(YesOrNo.YES.type);
        ordersMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 新增订单.
     *
     * @param order 订单
     */
    @Override
    public void insert(Orders order) {
        ordersMapper.insert(OrderCvt.INSTANCE.do2po(order));
    }

    /**
     * 根据用户id和订单状态查询订单总数
     *
     * @param userId      用户Id
     * @param orderStatus 订单状态
     * @return Integer 订单总数
     */
    @Override
    public Integer getOrderStatusCounts(String userId, Integer orderStatus) {
        // 1.查询用户的订单.
        List<Orders> myOrders = this.getMyOrders(userId);
        List<String> orderIds = myOrders.stream().map(Orders::getId).collect(Collectors.toList());
        // 2.根据我的订单列表以及状态查询"订单总数"
        Example example = new Example(OrderStatusPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderStatus", orderStatus);
        criteria.andIn("orderId", orderIds);
        return CollectionUtils.isEmpty(orderIds) ? 0 : orderStatusMapper.selectCountByExample(example);

    }

    /**
     * 根据用户id和订单状态查询订单状态列表.
     *
     * @param userId      用户Id
     * @param orderStatus 订单状态
     * @return 订单状态列表
     */
    @Override
    public PagedGridResult<MyOrdersVo> queryMyOrderByStatus(String userId, Integer orderStatus, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<MyOrdersVo> orderByUserIdAndStatus = ordersMapper.getOrderByUserIdAndStatus(userId, orderStatus);
        PageInfo<MyOrdersVo> myOrdersVoPageInfo = new PageInfo<>(orderByUserIdAndStatus);
        return PagedGridResult.setterPagedGrid(myOrdersVoPageInfo);
    }

    /**
     * 查询用户的订单动态
     *
     * @param userId 用户id
     * @return 订单状态列表
     */
    @Override
    public PagedGridResult<OrderStatus> getMyOrderTrend(String userId,Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<OrderStatus> myOrderTrend = ordersMapper.getMyOrderTrend(userId);
        PageInfo<OrderStatus> orderStatusPageInfo = new PageInfo<>(myOrderTrend);
        return PagedGridResult.setterPagedGrid(orderStatusPageInfo);
    }

    /**
     * 根据登录用户查询订单列表.
     *
     * @param userId 用户Id
     * @return 用户的订单列表
     */
    @Override
    public List<Orders> getMyOrders(String userId) {
        Example oderExample = new Example(OrdersPo.class);
        Example.Criteria orderCriteria = oderExample.createCriteria();
        orderCriteria.andEqualTo("isDelete", YesOrNo.NO.type);
        orderCriteria.andEqualTo("userId", userId);
        return OrderCvt.INSTANCE.po2doList(ordersMapper.selectByExample(oderExample));
    }


}
